package com.firesrc.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import jodd.db.DbQuery;
import jodd.db.DbThreadSession;
import jodd.db.oom.DbOomQuery;
import jodd.db.oom.sqlgen.DbEntitySql;

import com.firesrc.bean.MailHost;
import com.firesrc.dao.MailHostDao;
import com.firesrc.dao.SuperDao;
import com.firesrc.util.SystemUtil;

public class MailHostDaoImpl extends SuperDao<MailHost> implements MailHostDao {

	@Override
	public List<MailHost> getAllCurrentList() {
		DbThreadSession dbThreadSession = new DbThreadSession(cp);
		String date = SystemUtil.getCurrentDateStringYYYYMMDD();
		DbOomQuery query = new DbOomQuery(
				"select * from mailhost where state='1'  and mail in (select userName from mail_day_amount  where (AMOUNT<DAYMAX) and sendDate='"
						+ date + "') order by  seq"); //fixme 要把重复判断加上去
		List<MailHost> list = query.list(MailHost.class);
		dbThreadSession.closeSession();
		return list;
	}

	@Override
	public List<MailHost> getAllList(MailHost t) {
		DbThreadSession dbThreadSession = new DbThreadSession(cp);
		DbOomQuery query = new DbOomQuery("select * from mailhost  order by state desc, seq");
		List<MailHost> list = query.list(MailHost.class);
		dbThreadSession.closeSession();
		return list;
	}

	@Override
	public List<String> getAllCurrentKindList() {
		DbThreadSession dbThreadSession = new DbThreadSession(cp);
		ResultSet rs = new DbQuery(dbThreadSession, "select distinct kind from mailHost where state='1'").execute();
		LinkedList<String> linkedList = new LinkedList<String>();
		try {
			while (rs.next()) {
				linkedList.add(rs.getString(1));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return linkedList;
	}

	@Override
	public void enable(String id) {
		MailHost t = new MailHost();
		t.setId(id);
		updateColumn(t, "state", "1");
	}

	@Override
	public void disable(String id) {
		MailHost t = new MailHost();
		t.setId(id);
		updateColumn(t, "state", "0");
	}

	@Override
	public Map<String, Integer> getKindCountMap(String state) {
		DbThreadSession dbThreadSession = new DbThreadSession(cp);
		String sqlString;
		if (state != null) {
			sqlString = "select kind,count(id)  from mailHost where state='" + state + "'   group by kind";

		} else {
			sqlString = "select kind,count(id)  from mailHost  group by kind";

		}
		ResultSet rs = new DbQuery(dbThreadSession, sqlString).execute();
		Map<String, Integer> map = new HashMap<String, Integer>();
		try {
			while (rs.next()) {
				map.put(rs.getString(1), rs.getInt(2));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return map;
	}

	@Override
	public List<MailHost> getListByType(String type) {
		DbThreadSession dbThreadSession = new DbThreadSession(cp);
		List<MailHost> list = DbEntitySql.findByColumn(MailHost.class, "kind", type).query().list();
		dbThreadSession.closeSession();
		return list;
	}

	@Override
	public MailHost getById(String id) {
		MailHost mailHost = new MailHost();
		mailHost.setId(id);
		return getById(mailHost);
	}

	@Override
	public List<MailHost> getEnableList() {
		DbThreadSession dbThreadSession = new DbThreadSession(cp);
		DbOomQuery query = new DbOomQuery("select * from mailhost where state='1'   order by  seq");
		List<MailHost> list = query.list(MailHost.class);
		dbThreadSession.closeSession();
		return list;
	}

	@Override
	public int disableAll() {
		new DbThreadSession(cp);
		DbQuery query = new DbQuery("update mailhost set state='0' ");
		return query.executeUpdateAndClose();

	}

	@Override
	public int enableAll() {
		new DbThreadSession(cp);
		DbQuery query = new DbQuery("update mailhost set state='1' ");
		return query.executeUpdateAndClose();

	}

}
